package com.zhdl.modules.common.logging.web.controller;

import com.zhdl.common.util.WebResult;
import com.zhdl.modules.common.logging.entity.OptRecord;
import com.zhdl.modules.common.logging.service.OptRecordService;
import com.zhdl.modules.common.logging.web.req.OptRecordListRequest;
import com.zhdl.modules.common.logging.web.req.OptRecordReq;
import com.zhdl.modules.common.logging.web.resp.OptRecordListResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Slf4j
@RestController
@RequestMapping(path = "/optRecords", produces = MediaType.APPLICATION_JSON_VALUE)
public class OptRecordController {

    @Resource
    private OptRecordService optRecordService;

    @PostMapping("/get/optRecords/page/data/{pageSize}/{pageNumber}")
    public WebResult<Page<OptRecordListResp>> findAllRecord(@RequestBody @Validated OptRecordListRequest request, @PathVariable int pageNumber, @PathVariable int pageSize) {
        Pageable pageable = PageRequest.of(pageNumber, pageSize);
        Page<OptRecordListResp> allRecordPage = optRecordService.findAllRecordPage(request, pageable);
        return WebResult.okResult(allRecordPage);
    }

    @PostMapping("/optRecord/findAllPage")
    public WebResult<Page<OptRecord>> findAllRecord(@RequestBody OptRecordReq request) {
        Pageable pageable = PageRequest.of(request.getPageNumber(), request.getPageNumber());
        Page<OptRecord> allPage = optRecordService.findAllPage(request.getQueryCriteria(), pageable);
        return WebResult.okResult(allPage);
    }

    @GetMapping("/downloadCount")
    public void downloadCount(HttpServletResponse response) throws IOException {
//        optRecordService.saveRecords("用户-下载生产记录");
        optRecordService.download(optRecordService.selectAll(), response);
    }
}
